#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T> void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T> void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

const int maxn = 200010;
const int maxm = 15;

struct node {
  int u, S, ans;
} que[maxn];

int n, q, m, o, a[maxn], vis[maxn], fa[maxn], sz[maxn], mxsz[maxn], msk[maxn], bel[maxn], asz, rt, cnt[1 << maxm];
vector<int> e[maxn], all, vec[maxn];

void findrt(int u, int fa) {
  sz[u] = 1, mxsz[u] = 0;
  for (int v : e[u]) {
    if (v == fa || vis[v]) continue;
    findrt(v, u), sz[u] += sz[v];
    chkmx(mxsz[u], sz[v]);
  }
  chkmx(mxsz[u], asz - sz[u]);
  if (mxsz[u] <= asz / 2) rt = u;
}

void dfs(int u, int fa, int b) {
  bel[u] = b;
  sz[u] = 1, msk[u] = msk[fa] | (1 << a[u]);
  all.emplace_back(u);
  for (int v : e[u]) {
    if (v == fa || vis[v]) continue;
    dfs(v, u, b), sz[u] += sz[v];
  }
}

void solve(int u) {
  vis[u] = 1, msk[u] = (1 << a[u]);
  all.clear(), all.emplace_back(u);
  for (int v : e[u]) {
    if (vis[v]) continue;
    dfs(v, u, v), vec[v].clear();
  }
  for (int x : all) {
    int S = msk[x], hg = S >> o, ihg = ((1 << (m - o)) - 1) ^ hg;
    for (int is = ihg; ; is = (is - 1) & ihg) {
      cnt[((((1 << (m - o)) - 1) ^ is) << o) | (S & ((1 << o) - 1))]++;
      if (!is) break;
    }
  }
  for (int x : vec[u]) {
    if ((msk[que[x].u] & que[x].S) == msk[que[x].u]) {
      int S = que[x].S, lw = S & ((1 << o) - 1);
      for (int s = lw; ; s = (s - 1) & lw) {
        que[x].ans += cnt[((S >> o) << o) | s];
        if (!s) break;
      }
    } else {
      vec[bel[que[x].u]].emplace_back(x);
    }
  }
  for (int x : all) {
    int S = msk[x], hg = S >> o, ihg = ((1 << (m - o)) - 1) ^ hg;
    for (int is = ihg; ; is = (is - 1) & ihg) {
      cnt[((((1 << (m - o)) - 1) ^ is) << o) | (S & ((1 << o) - 1))]--;
      if (!is) break;
    }
  }
  for (int v : e[u]) {
    if (vis[v]) continue;
    asz = sz[v], findrt(v, u), swap(vec[v], vec[rt]), solve(rt);
  }
}

int main() {
  freopen("tree.in", "r", stdin);
  freopen("tree.out", "w", stdout);
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n >> q >> m, o = (m + 1) >> 1;
  rep (i, 1, n) cin >> a[i];
  rep (i, 2, n) cin >> fa[i];
  rep (i, 2, n) e[fa[i]].emplace_back(i);
  rep (i, 2, n) e[i].emplace_back(fa[i]);
  asz = n, findrt(1, 0);
  rep (i, 1, q) {
    cin >> que[i].S >> que[i].u, que[i].ans = 0;
    if (que[i].S >> a[que[i].u] & 1) vec[rt].emplace_back(i);
    else que[i].ans = 1;
  }
  solve(rt);
  rep (i, 1, q) cout << que[i].ans << '\n';
}